package cn.shenzz.hot100.leetcode_128;

import java.util.HashSet;
import java.util.Set;

class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) set.add(num);
        int res = 0;
        for (int num : nums) {
            if (!set.contains(num - 1)) {
                int current = num;
                int digit = 1;
                while (set.contains(current + 1)) {
                    ++current;
                    digit += 1;
                }
                res = Math.max(digit, res);
            }
        }

        return res;
    }
}